<?php

namespace App\Services\MiniprogramToGame\HotSearch;

use App\Services\ServiceBase;
use App\Structs\HotSearchStruct;
use Illuminate\Support\Facades\Http;

class Weibo extends ServiceBase
{
    public function reptile()
    {
        $url = 'https://weibo.com/ajax/side/hotSearch';
        $response = Http::get($url);
        if (!$response->ok()) {
            return false;
        }

        $data = json_decode($response->body(), true);
        if ($data['ok'] != 1) {
            return false;
        }

        $realtime = $data['data']['realtime'];
        $list = [];
        foreach ($realtime as $index => $item) {
            $class           = new HotSearchStruct;
            $class->title    = $item['note'];
            $class->hotIndex = $index+1;
            $class->hotValue = $item['num'];
            $class->url      = "https://s.weibo.com/weibo?q=".$item['note']."&Refer=index";
            $list[] = $class;
        }

        // 置顶
        $top_data = $data['data']['hotgov'] ?? [];
        if (!empty($top_data)) {
            $class           = new HotSearchStruct;
            $class->title    = $top_data['note'];
            $class->isTop    = 1;
            $class->url      = $top_data['url'];
            $list[]          = $class;
        }


        return $list;
    }
}
